package com.ruoyi.system.controller;

import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.system.domain.Consumer;
import com.ruoyi.system.domain.Vehicle;
import com.ruoyi.system.domain.dto.ConsumerDto;
import com.ruoyi.system.domain.dto.VehicleDto;
import com.ruoyi.system.service.ConsumerService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/consumer")
public class ConsumerController {
    @Resource
    ConsumerService consumerService;

    @PreAuthorize(hasPermi = "client:list")
    @PostMapping("/list")
    public AjaxResult list(@RequestBody ConsumerDto consumerDto) {
        return consumerService.ConsumerList(consumerDto);
    }

    /**
     * 修改客户
     */
    @PutMapping("/update")
    public AjaxResult edit(@RequestBody ConsumerDto consumerDto)
    {

        return consumerService.updateConsumer(consumerDto);
    }

    /**
     * 新增用户
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ConsumerDto consumerDto)
    {
        List<Consumer> list = consumerService.list();

        for (Consumer consumer : list) {
            if (consumer.getMpn().equals(consumerDto.getMpn())){
                return AjaxResult.error("手机号"+consumerDto.getMpn()+"已经注册");
            }
            if (consumer.getUsername().equals(consumerDto.getUsername())){
                return AjaxResult.error("账号"+consumerDto.getUsername()+"已经注册");
            }
        }

        return  consumerService.addConsumer(consumerDto);
    }
    @PostMapping("/det")
    public AjaxResult det(@RequestBody ConsumerDto consumerDto)
    {
        return  consumerService.detConsumer(consumerDto);
    }
}
